package com.ibm.productivity.tools.blogplugin.impl;

import java.util.ArrayList;

import com.ibm.productivity.tools.blogplugin.meta.Article;
import com.ibm.productivity.tools.blogplugin.meta.BlogException;
import com.ibm.productivity.tools.blogplugin.meta.Comment;
import com.ibm.productivity.tools.blogplugin.meta.IBlog;

public class MyMockBlog implements IBlog{

	private String blogName;
	private int id=0;
	 
	
	public int getNewId() {
		return id++;
	}
	
	ArrayList<Article> articles = new ArrayList<Article>();
	Comment []comments = new Comment[2];
	{	
		for (int i=0; i<4; ++i) {
			Article a = new Article("Article "+i, "This is a mock service.");
			a.setBlog(this);
			a.setId(getNewId());
			articles.add(a);
		}
		for (int i=0; i<comments.length; ++i) {
			comments[i] = new Comment("The is a comment mock.");
			comments[i].setArticle(articles.get(2));
		}
	}
	
	public MyMockBlog(String blogName) {
		this.blogName = blogName;
	}
	
	public boolean canDeleteArticle() {
		return true;
	}

	public boolean canDeleteComment() {
		return true;
	}

	public boolean canListArticle() {
		return true;
	}

	public boolean canListComment() {
		return true;
	}

	public boolean canPostArticle() {
		return true;
	}

	public boolean canPostComment() {
		return true;
	}

	public boolean canUpdateArticle() {
		return true;
	}

	public void deleteArticle(Article a) throws BlogException {
		int i;
		for (i=0; i<articles.size(); ++i)
			if (a.compareTo(articles.get(i))==0) {
				articles.remove(i);
				return ;
			}
		// not find
		throw new BlogException("Not find article.");
	}

	public void deleteComment(Article a, Comment comment) throws BlogException {
				
	}

	public int getArticleNumber() {
		return articles.size();
	}

	public Article[] getArticles() {
		Article [] res = new Article[articles.size()];
		res = articles.toArray(res);
		return res;
	}

	public Article[] getArticles(int number) {
		Article [] res = new Article[articles.size()];
		res = articles.toArray(res);
		return res;
	}

	public String getBlogName() {
		return blogName;
	}

	public Comment[] getComments(Article a) throws BlogException {
		if (a.getId()==1) {
			return comments;
		}	
		return null;
	}

	public String[] listArticle(int number) throws BlogException {
		String [] res = new String[articles.size()];
		for (int i=0; i<articles.size(); ++i)
			res[i] = new String(articles.get(i).getTitle());
		return res;
	}

	public Article postArticle(Article a) throws BlogException {
		a.setBlog(this);
		a.setId(getNewId());
		articles.add(a);
		return a;
	}

	public Comment postComment(Article a, String content) throws BlogException {
		Comment comment = new Comment(content);
		comment.setArticle(a);
		return comment;
	}

	public Article updateArticle(Article a) throws BlogException {
		for (int i=0; i<articles.size(); ++i)
			if (a.compareTo(articles.get(i))==0) {
				Article tmp = articles.get(i);
				tmp.setTitle(a.getTitle());
				tmp.setContent(a.getContent());
				return a;
			}
		// not find
		throw new BlogException("Not find article.");
		
	}

}
